#!/opt/bin/python2.7

import os
import shutil
import mimetypes
from optparse import OptionParser
import logging

import sys
sys.path.insert(0, os.path.dirname(__file__)+"/..")
egg_path='/usr/local/etc/Periscope/periscope-dev-py2.7.egg'

sys.path.append(egg_path)
import periscope

log = logging.getLogger(__name__)

SUPPORTED_FORMATS = 'video/x-msvideo', 'video/quicktime', 'video/x-matroska', 'video/mp4'

def main():
    '''Download subtitles'''
    # parse command line options
    parser = OptionParser("usage: %prog [options] file1 file2", version = periscope.VERSION)
    parser.add_option("-l", "--language", action="append", dest="langs", help="wanted language (ISO 639-1 two chars) for the subtitles (fr, en, ja, ...). If none is specified will download a subtitle in any language. This option can be used multiple times like %prog -l fr -l en file1 will try to download in french and then in english if no french subtitles are found.")
    parser.add_option("-f", "--force", action="store_true", dest="force_download", help="force download of a subtitle even there is already one present")
    parser.add_option("-i", "--interactive", action="store_true", dest="interactive", help="ask which subtitle to download")
    parser.add_option("-q", "--query", action="append", dest="queries", help="query to send to the subtitles website")
    parser.add_option("--lang-in-name", action="store_true", dest='lang_in_name', help="append lang to subtitle filename")
    parser.add_option("--cache-folder", action="store", type="string", dest="cache_folder", help="location of the periscope cache/config folder (default is ~/.config/periscope)")
    parser.add_option("--list-plugins", action="store_true", dest="show_plugins", help="list all plugins supported by periscope")
    parser.add_option("--list-active-plugins", action="store_true", dest="show_active_plugins", help="list all plugins used to search subtitles (a subset of all the supported plugins)")
    parser.add_option("--quiet", action="store_true", dest="quiet", help="run in quiet mode (only show warn and error messages)")
    parser.add_option("--debug", action="store_true", dest="debug", help="set the logging level to debug")
    (options, args) = parser.parse_args()


    # process args
    if options.debug :
        logging.basicConfig(level=logging.DEBUG)
    elif options.quiet :
        logging.basicConfig(level=logging.WARN)
    else :
        logging.basicConfig(level=logging.INFO)


    if not options.cache_folder:
        try:
            import xdg.BaseDirectory as bd
            options.cache_folder = os.path.join(bd.xdg_config_home, "periscope")
        except:
            home = os.path.expanduser("~")
            if home == "~":
                log.error("Could not generate a cache folder at the home location using XDG (freedesktop). You must specify a --cache-config folder where the cache and config will be located (always use the same folder).")
                exit()
            options.cache_folder = os.path.join("/usr/local/etc", ".config", "periscope")


    periscope_client = periscope.Periscope(options.cache_folder)

    if options.show_active_plugins:
        print "Active plugins: "
        plugins = periscope_client.listActivePlugins()
        for plugin in plugins:
            print "%s" %(plugin)
        exit()

    if options.show_plugins:
        print "All plugins: "
        plugins = periscope_client.listExistingPlugins()
        for plugin in plugins:
            print "%s" %(plugin)
        exit()

    if not args:
        print parser.print_help()
        exit()

    if options.queries: args += options.queries
    videos = []
    for arg in args:
        videos += recursive_search(arg, options)

    subs = []
    for arg in videos:
        if not options.langs: #Look into the config
            log.info("No lang given, looking into config file")
            langs = periscope_client.preferedLanguages
        else:
            langs = options.langs
        sub = periscope_client.downloadSubtitle(arg, langs, options.interactive)
        if sub:
            subs.append(sub)

    if options.lang_in_name is None:
        lang_in_name = periscope_client.preferedNaming
    else:
        lang_in_name = options.lang_in_name

    if lang_in_name:
        for s in subs:
            new_name = "%s.%s%s" % (os.path.splitext(s['subtitlepath'])[0], s['lang'], os.path.splitext(s['subtitlepath'])[1])
            shutil.move(s['subtitlepath'], new_name)
            s['subtitlepath'] = new_name

    log.info("*"*50)
    log.info("Downloaded %s subtitles" %len(subs))
    for s in subs:
        log.info(s['lang'] + " - " + s['subtitlepath'])
    log.info("*"*50)
    if len(subs) == 0:
        exit(1)


def recursive_search(entry, options):
    '''searches files in the dir'''
    files = []
    if os.path.isdir(entry):
        #TODO if hidden folder, don't keep going (how to handle windows/mac/linux ?)
        for e in os.listdir(entry):
            files += recursive_search(os.path.join(entry, e), options)
    elif os.path.isfile(entry):
        # Add mkv mimetype to the list
        mimetypes.add_type("video/x-matroska", ".mkv")
        mimetype = mimetypes.guess_type(entry)[0]
        if mimetype in SUPPORTED_FORMATS:
            # Add it to the list only if there is not already one (or forced)
            basepath = os.path.splitext(entry)[0]
            if options.force_download or not (os.path.exists(basepath+'.srt') or os.path.exists(basepath + '.sub')):
                files.append(os.path.normpath(entry))
            else:
                log.info("Skipping file %s as it already has a subtitle. Use the --force option to force the download" % entry)
        else :
            log.info("%s mimetype is '%s' which is not a supported video format (%s)" %(entry, mimetype, SUPPORTED_FORMATS))
    return files

if __name__ == "__main__":
    main()
